home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_quik.lha / quickdraw / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-09  |  8.3 KB  |  331 lines

  1. /*---------------------------------- main.c ----------------------------------*/
  2. /* Copyright 1989 Brown University -- Jeffrey Vogel                           */
  3. /*----------------------------------------------------------------------------*/
  4.  
  5. #define QD_BOSS
  6.  
  7.  
  8. /*--------------------------------- Includes ---------------------------------*/
  9. /*----------------------------------------------------------------------------*/
  10.  
  11. #include "qd_local.h"
  12. #include "patterns.bm"
  13. #include <X11/Xresource.h>
  14. #include <stdio.h>
  15.  
  16.  
  17. /*--------------------------------- Statics ----------------------------------*/
  18. /*----------------------------------------------------------------------------*/
  19.  
  20. static XrmDatabase QDDB;
  21.  
  22.  
  23.  
  24. /*----------------------------- GetSstringFromDb -----------------------------*/
  25. /*----------------------------------------------------------------------------*/
  26.  
  27. static
  28. char *
  29. GetStringFromDb(name)
  30. char *name;
  31. {
  32.    char        *str_type[20];
  33.    XrmValue    value;
  34.    char        *newval;
  35.  
  36.    XrmGetResource(QDDB, name, "Quickdraw.Attribute",
  37.                str_type, &value);
  38.    newval = (char *) malloc(strlen(value.addr) + 1);
  39.    strcpy(newval, value.addr);
  40.    return newval;
  41. }
  42.  
  43.  
  44.  
  45. /*---------------------------------- InitDb ----------------------------------*/
  46. /*----------------------------------------------------------------------------*/
  47.  
  48. static
  49. void
  50. InitDb()
  51. {
  52.    XrmDatabase user;
  53.  
  54.    XrmInitialize();
  55.    QDDB = XrmGetFileDatabase(X_DEFAULTS);
  56.    if (QDdisplay->xdefaults) {
  57.       user = XrmGetStringDatabase(QDdisplay->xdefaults);
  58.       XrmMergeDatabases(user, &QDDB);
  59.    }
  60.    F1 = GetStringFromDb("quickdraw.font1");
  61.    F2 = GetStringFromDb("quickdraw.font2");
  62.    F3 = GetStringFromDb("quickdraw.font3");
  63.    F4 = GetStringFromDb("quickdraw.font4");
  64. }
  65.  
  66.  
  67. /*-------------------------------- InitColor ---------------------------------*/
  68. /*----------------------------------------------------------------------------*/
  69.  
  70. static
  71. void
  72. InitColor()
  73. {
  74.    XColor   exact, color;
  75.  
  76.    /*** get the cmap ***/
  77.    QDcolormap = DefaultColormap(QDdisplay, QDscreen);
  78.  
  79.    /*** get all colors 8 ***/
  80.    XAllocNamedColor(QDdisplay, QDcolormap, "white",
  81.                     &exact, &color);
  82.    QDcolor__white = color.pixel;
  83.  
  84.    XAllocNamedColor(QDdisplay, QDcolormap, "black",
  85.                     &exact, &color);
  86.    QDcolor__black = color.pixel;
  87.  
  88.    XAllocNamedColor(QDdisplay, QDcolormap, "red",
  89.                     &exact, &color);
  90.    QDcolor__red = color.pixel;
  91.  
  92.    XAllocNamedColor(QDdisplay, QDcolormap, "green",
  93.                     &exact, &color);
  94.    QDcolor__green = color.pixel;
  95.    XAllocNamedColor(QDdisplay, QDcolormap, "blue",
  96.                     &exact, &color);
  97.    QDcolor__blue = color.pixel;
  98.    XAllocNamedColor(QDdisplay, QDcolormap, "cyan",
  99.                     &exact, &color);
  100.    QDcolor__cyan = color.pixel;
  101.    XAllocNamedColor(QDdisplay, QDcolormap, "yellow",
  102.                     &exact, &color);
  103.    QDcolor__yellow = color.pixel;
  104.    XAllocNamedColor(QDdisplay, QDcolormap, "magenta",
  105.                     &exact, &color);
  106.    QDcolor__magenta = color.pixel;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. /*--------------------------------- QDerror ----------------------------------*/
  113. /*----------------------------------------------------------------------------*/
  114.  
  115. void
  116. QDerror(str)
  117. char  *str;
  118. {
  119.    if (QDerrorHandler)
  120.       (*QDerrorHandler)(str);
  121.    else printf("%s\n", str);
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /*--------------------------------- QDreset ----------------------------------*/
  134. /*----------------------------------------------------------------------------*/
  135.  
  136. void
  137. QDreset()
  138. {
  139.    if (QDrunning)
  140.       QuitDraw();
  141. }
  142.  
  143.  
  144.  
  145. /*--------------------------------- QDclose ----------------------------------*/
  146. /*----------------------------------------------------------------------------*/
  147.  
  148. void
  149. QDclose()
  150. {
  151.    register int i;
  152.  
  153.    QDerrorHandler = NULL;
  154.  
  155.    /*** kill the data structures ***/
  156.    for (i = 0; i < QD__MAX_FONTS; i++)
  157.       XFreeFont(QDdisplay, QDfontInfo[i]);
  158.    XFreeGC(QDdisplay, QDgc);
  159.    XFreeGC(QDdisplay, QDinvertgc);
  160.    XFreeGC(QDdisplay, QDgridgc);
  161.    XFreePixmap(QDdisplay, QDstipple__dkGray);
  162.    XFreePixmap(QDdisplay, QDstipple__ltGray);
  163.    XFreePixmap(QDdisplay, QDstipple__gray);
  164.    XFreePixmap(QDdisplay, QDstipple__white);
  165.    XFreePixmap(QDdisplay, QDstipple__black);
  166.  
  167.    /*** close display ***/
  168.    XCloseDisplay(QDdisplay);
  169.  
  170.    QDautoDisplay = FALSE;
  171.    QDdisplay = NULL;
  172. }
  173.  
  174.  
  175. /*---------------------------------- QDopen ----------------------------------*/
  176. /*----------------------------------------------------------------------------*/
  177.  
  178. void
  179. QDopen(display, errorHandler)
  180. char        *display;
  181. FunctionPtr errorHandler;
  182. {
  183.    int         depth;
  184.    Status      retval;
  185.    XColor      color;
  186.    Font        f;
  187.  
  188.    /*** open server ***/
  189.    QDdisplay = XOpenDisplay(display);
  190.    if (QDdisplay == NULL) {
  191.        QDerror("ERROR QDopen: Unable to open display.");
  192.        return;
  193.    }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.    /*** set variables ***/
  200.    QDerrorHandler = errorHandler;
  201.    XSynchronize(QDdisplay, FALSE);
  202.    QDscreen  = DefaultScreen(QDdisplay);
  203.    QDroot    = RootWindow(QDdisplay, QDscreen);
  204.  
  205.    /*** get QDDB ***/
  206.    InitDb();
  207.  
  208.    /*** init seed ***/
  209.    srandom(1);
  210.  
  211.    /*** load fonts ***/
  212.    f = QDfonts[FONT__SMALL] = XLoadFont(QDdisplay, F1);
  213.    if (!f) {
  214.       QDerror("ERROR QDopen: Unable to open fonts.");
  215.       return;
  216.    }
  217.    f = QDfonts[FONT__MEDIUM] = XLoadFont(QDdisplay, F2);
  218.    if (!f) {
  219.       QDerror("ERROR QDopen: Unable to open fonts.");
  220.       return;
  221.    }
  222.    f = QDfonts[FONT__LARGE] = XLoadFont(QDdisplay, F3);
  223.    if (!f) {
  224.       QDerror("ERROR QDopen: Unable to open fonts.");
  225.       return;
  226.    }
  227.    f = QDfonts[FONT__LARGEST] = XLoadFont(QDdisplay, F4);
  228.    if (!f) {
  229.       QDerror("ERROR QDopen: Unable to open fonts.");
  230.       return;
  231.    }
  232.    QDfontInfo[FONT__SMALL] = XQueryFont(QDdisplay, QDfonts[FONT__SMALL]);
  233.    QDfontInfo[FONT__MEDIUM] = XQueryFont(QDdisplay, QDfonts[FONT__MEDIUM]);
  234.    QDfontInfo[FONT__LARGE] = XQueryFont(QDdisplay, QDfonts[FONT__LARGE]);
  235.    QDfontInfo[FONT__LARGEST] = XQueryFont(QDdisplay, QDfonts[FONT__LARGEST]);
  236.  
  237.  
  238.    /*** create the GC ***/
  239.    QDgcValues.function   = FUNCTION__DEFAULT;
  240.    QDgcValues.line_width = LINE_WIDTH__DEFAULT;
  241.    QDgcValues.line_style = LINE_STYLE__DEFAULT;
  242.    QDgcValues.font       = QDfonts[FONT__DEFAULT];
  243.    QDgcValues.foreground = BlackPixel(QDdisplay, QDscreen);
  244.    QDgcValues.background = WhitePixel(QDdisplay, QDscreen);
  245.    QDgcValues.graphics_exposures = FALSE;
  246.    QDgc      = XCreateGC (QDdisplay, QDroot,
  247.                           GCFont|GCFunction|GCLineWidth|GCLineStyle|
  248.                           GCForeground|GCBackground|
  249.                           GCGraphicsExposures|GCFont, 
  250.                           &QDgcValues);
  251.  
  252.  
  253.    /*** init color stuff ***/
  254.    depth = DefaultDepth(QDdisplay, QDscreen);
  255.    QDcolor = (depth > 1);
  256.    if (QDcolor) {
  257.       InitColor();
  258.    }
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.    /*** create other gcs ***/
  266.    QDinvertgc         = XCreateGC (QDdisplay, QDroot, NULL, NULL);
  267.    XSetFunction(QDdisplay, QDinvertgc, GXxor);
  268.    XSetForeground(QDdisplay, QDinvertgc, BlackPixel(QDdisplay, QDscreen));
  269.    XSetBackground(QDdisplay, QDinvertgc, WhitePixel(QDdisplay, QDscreen));
  270.    XSetFillStyle(QDdisplay, QDinvertgc, FillSolid);
  271.  
  272.    /*** grid gc ***/
  273.    QDgridgc           = XCreateGC (QDdisplay, QDroot, 
  274.                                    GCGraphicsExposures,
  275.                                    &QDgcValues);
  276.    XSetForeground(QDdisplay, QDgridgc, BlackPixel(QDdisplay, QDscreen));
  277.    XSetBackground(QDdisplay, QDgridgc, WhitePixel(QDdisplay, QDscreen));
  278.    XSetFillStyle(QDdisplay, QDgridgc, FillSolid);
  279.    XSetLineAttributes(QDdisplay, QDgridgc, 1,
  280.                       LineOnOffDash, CapNotLast, JoinMiter);
  281.  
  282.    /*** add patterns ***/
  283.    QDstipple__dkGray = 
  284.      XCreateBitmapFromData(QDdisplay, QDroot, bitpat_1, 8, 8);
  285.    QDstipple__ltGray = 
  286.      XCreateBitmapFromData(QDdisplay, QDroot, bitpat_23, 8, 8);
  287.    QDstipple__gray = 
  288.      XCreateBitmapFromData(QDdisplay, QDroot, bitpat_3, 8, 8);
  289.    QDstipple__white = 
  290.      XCreateBitmapFromData(QDdisplay, QDroot, bitpat_38, 8, 8);
  291.    QDstipple__black = 
  292.      XCreateBitmapFromData(QDdisplay, QDroot, bitpat_0, 8, 8);
  293. }
  294.  
  295.  
  296.  
  297.  
  298.  
  299. /*----------------------------- QDallowKeypress ------------------------------*/
  300. /*----------------------------------------------------------------------------*/
  301.  
  302. void
  303. QDallowKeypress()
  304. {
  305.    if (!QDrunning) 
  306.       return;
  307.    XSelectInput(QDdisplay, QDwindow, StructureNotifyMask|KeyPressMask);
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.